<template>
	<view class="pages-shop">
		<fixed :zIndex="99">
			<uni-nav-bar :fixed="false" :shadow="false" :statusBar="true" title="商城" color="#fff"
				backgroundColor="none">
			</uni-nav-bar>
			<view class="shop-search-info space-shop abs">
				<view mode="aspectFill" lazy-load class="breed-index-bg abs"></view>
				<view class="search-item flex-between abs">
					<view style="width: 604rpx;">
						<search @input="toSearch" type="input" :padding="0" :radius="70"
							backgroundColor="rgba(240,240,240,0.2)" searchColor="rgba(240,240,240,0.2)"
							frontColor="#fff" placeholder="搜索商品名称"></search>
					</view>
					<image @tap.stop="$util.toCheckLogin({url:`/shop/pages/car`})" mode="aspectFill" lazy-load
						class="car-img" src="/static/image/shop/car-img.png"></image>
					<view v-if="shopCarList.car_count" class="car-count flex-center c-base f-icontext abs"
						:style="{width: shopCarList.car_count<10 ? '30rpx' : '50rpx'}">
						{{shopCarList.car_count < 100 ? shopCarList.car_count : '99+'}}
					</view>
				</view>
				<view class="cate-item abs fill-base flex-between b-1px-b" v-if="tabList.length>0">
					<view style="width: calc(100% - 132rpx)">
						<tab @change="handerTabChange($event,'activeIndex')" :list="tabList" :activeIndex="activeIndex"
							:activeColor="primaryColor" :haveLine="false" fontSize="28rpx" height="80rpx"></tab>
					</view>
					<view @tap.stop="toShowRank" class="flex-center f-caption c-desc b-1px-l" style="width: 132rpx;">
						更多
						<i class="iconfont icon-gengduo ml-sm"></i>
					</view>
				</view>
			</view>
		</fixed>

		<uni-popup @change="popupChange" ref="rank_item" type="top" :custom="true">
			<view :style="{height: `${shopHeight}px`}"></view>
			<view class="pd-lg fill-base">
				<view class="f-title c-title text-bold pb-md">类别</view>
				<view class="rank-list flex-warp">
					<view @tap.stop="handerTabChange(index,'activeIndex')"
						class="rank-item flex-center mt-md f-paragraph c-desc" :class="[{'cur':index==activeIndex}]"
						:style="{color:index==activeIndex?primaryColor:''}" v-for="(item,index) in tabList"
						:key="index">
						{{item.title}}
					</view>
				</view>
			</view>
		</uni-popup>

		<view class="space-shop rel" :style="{height: (180 - configInfo.navBarHeight) + 'px'}"></view>
		<view class="flex-warp">
			<view @tap.stop="goDetail(index)" class="goods-item fill-base mt-md ml-md box-shadow radius-16"
				v-for="(item,index) in list.data" :key="index">
				<image mode="aspectFill" lazy-load class="cover" :src="item.cover"></image>
				<view class="pd-md">
					<view class="f-desc c-title text-bold ellipsis">{{item.goods_name}}</view>
					<view class="flex-y-center f-caption ellipsis">
						<view class="flex-y-center c-warning">¥<view class="f-md-title ellipsis">{{item.show_price}}
							</view>
						</view>
						<view class="c-caption ml-sm">¥{{item.show_init_price}}</view>
					</view>
					<view class="f-caption c-caption">销量 {{item.all_sale_count}}</view>
					<view class="flex-center f-caption c-caption">
						<image mode="aspectFill" lazy-load class="store-img radius" :src="item.store_cover"></image>
						<view class="flex-1 ml-sm ellipsis">{{item.store_name}}</view>
					</view>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>
		<view class="space-footer"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				isLoad: false,
				options: {},
				storeInfo: {},
				tabList: [],
				activeIndex: 0,
				cate_id: 0,
				shopHeight: 0,
				param: {
					page: 1,
					cate_id: 0
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false,
				show_rank_item: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
			shopCarList: state => state.order.shopCarList,
			loginType: state => state.user.loginType,
		}),
		async onLoad() {
			this.$util.showLoading()
			await this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
			if (!this.show_rank_item) return
			this.$refs.rank_item.close()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			...mapActions(['getConfigInfo', 'updateCommonOptions', 'getShopCarList']),
			...mapMutations(['updateUserItem']),
			async initIndex(refresh = false) {
				if (!this.configInfo.id || refresh) {
					await this.getConfigInfo()
					if (this.loginType == 'apple') {
						this.updateUserItem({
							key: 'isShowLogin',
							val: false
						})
					}
				}
				let {
					cate_id,
				} = await this.$api.shop.goodsIndex()
				cate_id.unshift({
					id: 0,
					title: '全部'
				})
				this.isLoad = true
				this.tabList = cate_id
				this.param.page = 1
				await Promise.all([this.getShopCarList(), this.getList()])
				this.toSetResHeight()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			async getList() {
				let {
					list: oldList,
					tabList,
					activeIndex,
					cate_id
				} = this
				let ind = tabList.findIndex(item => {
					return item.id == cate_id
				})
				cate_id = ind == -1 ? 0 : cate_id
				activeIndex = ind == -1 ? 0 : ind
				this.cate_id = cate_id
				this.activeIndex = activeIndex
				let param = Object.assign({}, this.param, {
					cate_id,
				})
				let newList = await this.$api.shop.goodsList(param)
				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			toSearch(val) {
				this.loading = true
				this.param.page = 1
				this.param.goods_name = val
				this.getList()
			},
			async toSetResHeight() {
				const query = uni.createSelectorQuery();
				query.select('.shop-search-info').boundingClientRect(data => {
					this.shopHeight = data.height - 40
				}).exec();
			},
			toShowRank() {
				let {
					show_rank_item,
				} = this
				if (this.lockTap) return
				this.lockTap = true
				setTimeout(() => {
					let methodModel = show_rank_item ? 'close' : 'open'
					this.$refs.rank_item[methodModel]()
				}, 500)
			},
			popupChange(e) {
				let {
					show
				} = e
				this.show_rank_item = show
				setTimeout(() => {
					this.lockTap = false
				}, 200)
			},
			handerTabChange(index, type) {
				this[type] = index
				this.$refs.rank_item.close()
				if (type == 'activeIndex') {
					this.cate_id = this.tabList[index].id
				}
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			goDetail(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/shop/pages/detail?id=${id}`
				this.$util.toCheckLogin({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
	.pages-shop {
		.shop-search-info {
			z-index: -1;

			.search-item {
				width: 100%;
				padding: 0 25rpx;
				top: 180rpx;

				.car-img {
					width: 64rpx;
					height: 64rpx;
				}

				.car-count {
					width: 30rpx;
					height: 30rpx;
					line-height: 30rpx;
					border-radius: 15rpx;
					background: #f12c20;
					top: -10rpx;
					right: 10rpx;
				}
			}

			.cate-item {
				width: 100%;
				bottom: 0;

				.iconfont {
					font-size: 24rpx;
				}
			}
		}

		.space-shop {
			width: 100%;
			height: 360rpx;
			top: 0;
			left: 0;
			height: 360rpx;
		}

		.goods-item {
			width: 345rpx;
			height: 550rpx;
			overflow: hidden;

			.cover {
				width: 345rpx;
				height: 350rpx;
			}

			.store-img {
				width: 26rpx;
				height: 26rpx;
			}
		}
		
		.breed-index-bg {
			background: rgb(57, 181, 74);
			height: 10rem;
			width: 140%;
			left: -20%;
			top: 0;
			z-index: -1;
		}
		
		.rank-list {
			.rank-item {
				width: 215rpx;
				height: 78rpx;
				background: #F5F5F5;
				border-radius: 8rpx;
				margin-right: 22rpx;
			}

			.rank-item:nth-child(3n) {
				margin-right: 0;
			}

			.cur {
				background: rgba(0, 146, 84, 0.1);
			}
		}
	}
</style>
